<?php
/*
 * Ruffy.php - Main file for the Ruffy IRC Bot
 * Written by Nick Markwell
 *
 * Part of the Ruffy IRC Bot
 * http://rockermono.nonlogic.org/projects/
 *
 * Ruffy is slightly based on the connection portions of the Xando/BONO IRC Bots
 *
 */

//error_reporting(E_ERROR | E_WARNING | E_PARSE); // This will NOT report uninitialized variables ,etc

declare(ticks=1);

$debug = false;

if (!empty($argv[1]))
	include($argv[1]);
else 
	include("ruffy.config.php");

$lastMessages = "";
$floodIgnores=Array();

$startTime=microtime(true);

$version=7.2;
$verStr=(string)$version;
if(!stristr($verStr,".")){
  $version=number_format($version,1);
  $verStr=(string)$version;
}
$useColors=true;
$commandSent=false;
$allCommands=Array();
$antiFloodGlobal=0;
$antiFloodGlobalTime=time();
$antiFlood=Array();
$antiFloodTime=Array();

include('logging.php');
include("functions.php");
include('signals.php');
include("controls/memory.php");
include("controls/common.php");

file_put_contents("data/ruffy-$network.pid", getmypid());

// The Access levels Array, probably wise to not edit it directly
if(file_exists("data/access.dat")){
  $levels=unserialize(file_get_contents("data/access.dat"));
}

$channels=$defaultChannels;

$allCommands="";
$originalNick=$nickname;
$checkWhois=false;
$ghosted=false;

while(true)
{
	if(!empty($nickname)){
		$server_host = $network;
		if(empty($port)){
			$server_port=6667;
		} else {
			$server_port=$port;
		}

		$joined=false;

    //$server = array();
	report(">!<", "Connecting to $server_host:$server_port...");
    $socket = @fsockopen($server_host, $server_port, $errno, $errstr, 2);
    if(!$socket)
    {
		report(">!<", "Unable to connect to $server_host:$server_port.");
    }
    else
	{
		report(">!<", "Connected to $server_host:$server_port.");
      send("NICK $nickname");
      send("USER $nickname * * :Ruffy v$version by duck, heavily edited by danopia");
      while(!@feof($socket) && $socket != -1)
      {
        $data = @fgets($socket, 1024); //get a line of data from the server
		    $data = str_replace(array("\n", "\r"), '', $data);           
    		$data = str_replace(chr(7), '*', $data);
			log_recv($data);
				
				if ($data == '')
					send('PING :TIMEOUTCHECK');
				
				include('parser.php'); // Danopia's parsing engine.
				
				if(array_key_exists($source_host,$levels))
					$level = $levels[$source_host];
				else
					$level = 0;
				
				if($command == '1') {
					send("MODE $nickname +B");
					send('PRIVMSG Nickserv :identify '.$password[strtolower($network)],true);
					send('JOIN '.implode(',',$channels));
					$joined = true;
				} else if($command == '433') {
					$nickname .= '`';
					send("NICK $nickname");
				} else if($command == 'PING') {
					send("PONG :$message");
				} else if(($command == 'NICK') && ($source_nick == $nickname)) {
					$nickname = $message;
				}
				
				if ($joined) {
				
					// Handle the modules variables
					$modules = array();
					$current_module = '';
					
					foreach(dirlist('./modules/',true) as $module){
						if(substr($module,strlen($module)-4)=='.php' and substr($module,strlen('./modules/'),1)!='_'){
							$moduleText=file_get_contents($module);
							$moduleText=substr($moduleText,5,strlen($moduleText)-7);
							
							// Handle the modules variables
							$temp = explode('/', $module);
							$current_module = substr($temp[2], 0, strlen($temp[2])-4);
							$modules[$current_module] = array();

							// Way too much debugging!
							//log_debug($current_module)
							
							try{
								//eval($moduleText);
								require($module);
							} catch(Exception $e){
								log_debug("$module: " . $e->getMessage());
							}
							//include($module);
						}
					}
					
					if ($msg_cmd == 'help') include('help.php');
				}
				
				$uFloodTime=time();
				$uFloodTime-=10;
				if(!is_array($antiFlood)) $antiFlood=Array();
				if(!is_array($antiFloodTime)) $antiFloodTime=Array();
				if(!array_key_exists($source_nick,$antiFloodTime)) $antiFloodTime[$source_nick]=time();
				if(!array_key_exists($source_nick,$antiFlood)) $antiFlood[$source_nick]=0;
				
				if($antiFloodTime[$source_nick]<=$uFloodTime){
					if($antiFlood[$source_nick]>0){
						$antiFlood[$source_nick]--;
					} else {
						$antiFloodTime[$source_nick]=time();
					}
				}
				if($antiFloodGlobalTime<=$uFloodTime){
					if($antiFloodGlobal>0) $antiFloodGlobal--;
				}
				
				flush(); // This is for when Ruffy is run in your Web Browser.
			}
		}
}

report(">!<", "Connection lost. Reconnecting in 10 seconds.");
// no instant reconnects! wait 10 seconds so it wont freeze your computer or get klined 
sleep(10);
}
?>
